<?php

namespace App\Http\Controllers\Manager;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Manager;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use App\Models\Order;
use App\Models\User;

class ManagerController extends Controller
{
    //过滤非法用户
    public function __construct()
    {
//        $this->middleware('admin');
        $this->middleware('auth:web');
    }

    //管理员主页
    public function index($option)
    {
        $auth = Auth::guard('web')->user();

        if ($option == 0)
        {
            $result = Order::where('id','>','0');//筛选条件

            //实现分页功能，1页10行
            $count = $result->count();
            $orders = $result->paginate(10);
            return view('Manager.manager1', compact(['orders','auth'], 'count'));
        }
        else
        {
            $result = User::where('id','>','0');  //筛选条件

            //实现分页功能，1页10行
            $count = $result->count();
            $users = $result->paginate(10);
            return view('Manager.manager2', compact(['users','auth'], 'count'));
        }

    }

    //订单删除功能,用户封禁(删除)
    public function destroy(Order $order,User $user)
    {
        if( $order->id !=0 )    //订单删除
        {
            $name = $order->title;
            $order->delete();
            session()->flash('message','订单 '.$name.'已被删除');
            return Redirect::back();
        }
        else    //用户删除
        {
            $name = $user->name;
            $user->delete();
            session()->flash('message','用户 '.$name.'已被删除');
            return Redirect::back();
        }
    }

}
